package deibay.client.gui;

import java.awt.event.ActionListener;

import org.jdesktop.swingx.JXFrame;

import deibay.client.core.views.IViewRegister;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.UIManager;

public class RegisterView extends JXFrame implements IViewRegister{

	private JPanel pane = null;
	private JLabel userLabel = null;
	private JLabel addressLabel = null;
	private JLabel emailLabel = null;
	private JLabel passLabel = null;
	private JTextField userField = null;	
	private JTextField emailField = null;
	private JTextField passwordField = null;
	private JTextArea addressField = null;
	private JCheckBox roleCheck = null;	
	private JScrollPane addressScroll = null;
	private JButton registerButton = null;
	public RegisterView()
	{
		super();
		initialize();		
	}
	
	private void initialize() {
		setTitle("Register");
		this.setContentPane(getPane());
		setSize(350, 370);	
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setVisible(true);
	}
	
	/**
	 * This method initializes pane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPane() {
		if (pane == null) {
			userLabel = new JLabel();
			userLabel.setBounds(new Rectangle(30, 30, 38, 16));
			userLabel.setText("User ");
			passLabel = new JLabel();
			passLabel.setBounds(new Rectangle(30, 70, 63, 16));
			passLabel.setText("Password ");
			emailLabel = new JLabel();
			emailLabel.setBounds(new Rectangle(30, 110, 38, 16));
			emailLabel.setText("Email ");
			addressLabel = new JLabel();
			addressLabel.setBounds(new Rectangle(30, 150, 54, 16));
			addressLabel.setText("Address ");			
			pane = new JPanel();
			pane.setLayout(null);
			pane.add(userLabel, null);
			pane.add(addressLabel, null);
			pane.add(emailLabel, null);
			pane.add(passLabel, null);
			pane.add(getUserField(), null);					
			pane.add(getEmailField(), null);
			pane.add(getRoleCheck(), null);			
			pane.add(getAddressScroll(), null);
			pane.add(getPasswordField(), null);
			pane.add(getRegisterButton(), null);
		}
		return pane;
	}

	/**
	 * This method initializes userField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getUserField() {
		if (userField == null) {
			userField = new JTextField();
			userField.setBounds(new Rectangle(100, 30, 200, 20));
		}
		return userField;
	}
	
	/**
	 * This method initializes emailField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEmailField() {
		if (emailField == null) {
			emailField = new JTextField();
			emailField.setBounds(new Rectangle(100, 110, 200, 20));
		}
		return emailField;
	}

	/**
	 * This method initializes roleCheck	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getRoleCheck() {
		if (roleCheck == null) {
			roleCheck = new JCheckBox();			
			roleCheck.setText(" Seller Role");
			roleCheck.setBounds(new Rectangle(99, 270, 200, 20));
		}
		return roleCheck;
	}

	/**
	 * This method initializes addressField	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getAddressField() {
		if (addressField == null) {
			addressField = new JTextArea();			
		}
		return addressField;
	}

	/**
	 * This method initializes addressScroll	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getAddressScroll() {
		if (addressScroll == null) {
			addressScroll = new JScrollPane();
			addressScroll.setViewportView(getAddressField());
			addressScroll.setBounds(new Rectangle(100, 150, 200, 60));
		}
		return addressScroll;
	}

	/**
	 * This method initializes passwordField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JTextField();
			passwordField.setBounds(new Rectangle(100, 70, 200, 20));
		}
		return passwordField;
	}

	/**
	 * This method initializes registerButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRegisterButton() {
		if (registerButton == null) {
			registerButton = new JButton();
			registerButton.setText("Register");
			registerButton.setBounds(new Rectangle(200, 300, 100, 25));
		}
		return registerButton;
	}

	public String getAddress() {		
		return addressField.getText();
	}

	public String getEmail() {
		return emailField.getText();
	}

	public String getPassword() {
		return passwordField.getText();
	}

	public String getUsername() {
		return userField.getText();
	}

	public void setRegisterButtonAction(ActionListener listener) {
		registerButton.addActionListener(listener);
	}

	public String getRole() {
		if(roleCheck.isSelected())
			return "seller";
		else return "bidder";		
	}

	public void close() {		
		setVisible(false);
	}
	
}
